import {createWebHashHistory, createRouter} from 'vue-router'

import Layout from '../page/layout/index.vue'
import Home from '../page/home/index.vue'
import Problem from "../page/problem/index.vue"
import ProblemAdd from "../page/problem/add.vue"
import PracticeList from "../page/practice/list.vue"
import PracticeRedo from "../page/practice/redo.vue"
import Login from "../page/login/index.vue"
import UserDetail from "../page/user/detail.vue"
import UserUpdateDetail from "../page/user/update_detail.vue"
import Error403 from "../page/error/error403.vue"
import Error404 from "../page/error/error404.vue"
import StudentCourseList from "../page/student/course_list.vue"
import StudentRecordAdd from "../page/student/record_add.vue"
import StudentRecordList from "../page/student/record_list.vue"
import StudentAdd from "../page/student/student_add.vue"
import StudentList from "../page/student/student_list.vue"
import FileIndex from "../page/file/index.vue"
import FileUpload from "../page/file/upload.vue"
import FileMusicList from "../page/file/music_list.vue"
import storage from "../util/storage.js";

const routes = [
    {
        path: '/',
        component: Layout,
        redirect: '/home',
        children: [
            {path: '/home', component: Home},
            {path: '/problem/list', component: Problem},
            {path: '/problem/add', component: ProblemAdd},
            {path: '/practice/list', component: PracticeList},
            {path: '/practice/redo', component: PracticeRedo},
            {path: '/user/detail', component: UserDetail},
            {path: '/user/update_detail', component: UserUpdateDetail},
            {path: '/student/course_list', component: StudentCourseList},
            {path: '/student/record_add', component: StudentRecordAdd},
            {path: '/student/record_list', component: StudentRecordList},
            {path: '/student/add', component: StudentAdd},
            {path: '/student/list', component: StudentList},
            {path: '/file/index', component: FileIndex},
            {path: '/file/upload', component: FileUpload},
            {path: '/file/music_list', component: FileMusicList},
        ]
    },
    {
        path: "/login",
        component: Login,
    },
    {
        path: "/403",
        component: Error403,
    },
    {
        path: "/404",
        component: Error404,
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

const whiteUrls = [
    "/",
    "/home",
    "/login",
    "/403",
    "/404",
    "/500",
    "/problem/list",
    "/practice/list",
    "/user/detail",
    "/user/update_detail",
]

const authUrls = [
    "/problem/add",
    "/practice/redo",
    "/student/course_list",
    "/student/record_add",
    "/student/add",
    "/student/list",
    "/student/record_list",
    "/file/index",
    "/file/upload",
    "/file/music_list",
]

function hasAuth(authList, name) {
    console.log(authList, name)
    if (!authList || !name) {
        return false
    }
    if (whiteUrls.indexOf(name) !== -1) {
        return true
    }
    for (let i = 0; i < authList.length; i++) {
        if (authList[i].name === name) {
            return true
        }
    }
    return false
}

router.beforeEach((to, from, next) => {
    // validate 404
    if (whiteUrls.indexOf(to.path) === -1 && authUrls.indexOf(to.path) === -1) {
        return next({path: "/404"})
    }

    // validate auth
    if (whiteUrls.indexOf(to.path) === -1) {
        const authList = storage.get("auth")
        if (!hasAuth(authList, to.path)) {
            return next({path: "/403"})
        }
    }

    const user = storage.get("user")
    if ((!user || !user.token) && to.path !== "/login") {
        return next({path: "/login"})
    }
    next()
})


export default router